package com.dlink.sqljobsync.autoconfig;

import com.dlink.sqljobsync.event.KafkaNoticeEvent;
import com.dlink.sqljobsync.listener.CustomCommandLineRunner;
import com.dlink.sqljobsync.propertie.DlinkSqlProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 *
 * @author huangjun
 */
@EnableConfigurationProperties(DlinkSqlProperties.class)
@ConditionalOnProperty(prefix = "dlinky.auto", name = "enable", havingValue = "true")
@Slf4j
public class SqlJobAutoConfig {
    @Bean
    CommandLineRunner createCustomCommandLineRunner(Environment env){
        return new CustomCommandLineRunner(env);
    }

    /**
     * 自动装配初始化

    @PostConstruct
    public void initTool(){
        DlinkSyncHttpTool.setSqlJobAutoConfig(this);
    }
     */
    /**
     * 创建执行任务池
     * @param taskExecutor
     * @return
     */
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster CustomApplicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor){
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(taskExecutor);
        return simpleApplicationEventMulticaster;
    }


    /**
     * 管理所有通用事件调用
     */
    public static ApplicationEvent noticeEvent;

}
